#include "static_sprite.h"


StaticSprite::StaticSprite(void) {

}


StaticSprite::StaticSprite(int textureId) {
  // get texture node
  TexturesManager* texturesManager = TexturesManager::getInstance();
  textureNode = texturesManager->GetTextureById(textureId);
  CString stringId;
  stringId.Format(L"%d",textureId);
  if(textureNode == NULL) {
    Log::Write(L"Get texture from texture manager false; ID=",
          (LPWSTR)stringId.GetString()) ;
  }
    // Create sprite handeler
  HRESULT result = D3DXCreateSprite(
      Screen::screen->getDevive(), 
      &spriteHandler);
  // Log init sprite
  if(result != D3D_OK) {
     Log::Write(L"Create sprite handeler false.");
     return;
  }
}


StaticSprite::~StaticSprite(void) {

}



void StaticSprite::Draw(RECT *rect, D3DXVECTOR3 *vectorPosition) {
  // Lock sprite object for drawing
  this->spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);
  this->spriteHandler->Draw(
    this->textureNode->texture,
    rect,
    NULL,
    vectorPosition,
    D3DCOLOR_XRGB(255,255,255));
  // Unlock sprte after drawing
  this->spriteHandler->End();
}


void StaticSprite::Scale(float value) {
  // Transform sprite
  D3DXMATRIX mat;
  D3DXVECTOR2 scale(value, value);
  D3DXMatrixTransformation2D(&mat,NULL,0,&scale, 0, 0, 0);
  spriteHandler->SetTransform(&mat);
}


void StaticSprite::SetTexture(int textureId) {
  TexturesManager* texturesManager = TexturesManager::getInstance();
  textureNode = texturesManager->GetTextureById(textureId);
  CString stringId;
  stringId.Format(L"%d",textureId);
  if(textureNode == NULL) {
    Log::Write(L"Get texture from texture manager false; ID=",
          (LPWSTR)stringId.GetString()) ;
  }
}


void StaticSprite::Release(void) {
  if(this->spriteHandler != NULL)
    this->spriteHandler->Release();
}